package com.exam.interceptor;


import com.exam.exception.AuthorizationException;
import com.exam.exception.BaseException;
import com.exam.util.StringUtils;
import com.exam.vo.examEnum.ResultCode;
import com.exam.vo.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionAdvice {

	private static final Logger log = LoggerFactory.getLogger(ExceptionAdvice.class);

	@ExceptionHandler({ BaseException.class })
	public ResultData<Void> cauthException(BaseException e) {
		log.error(e.getMessage(), e);
		ResultData<Void> data = new ResultData<>();
		data.setCode(e.getResultCode().getCode());
		data.setMsg(e.getMessage());
		return data;
	}

	@ExceptionHandler({ AuthorizationException.class })
	public ResultData<Void> cautchException(AuthorizationException e) {
		log.error(e.getMessage(), e);
		ResultData<Void> data = new ResultData<>();
		data.setCode(e.getResultCode().getCode());
		data.setMsg(e.getMessage());
		return data;
	}

	@ExceptionHandler({ IllegalArgumentException.class })
	public ResultData<Void> cautchException(IllegalArgumentException e) {
		log.error(e.getMessage(), e);
		ResultData<Void> data = new ResultData<>();
		data.setCode(ResultCode.BAD_REQUEST.getCode());
		String msg = e.getMessage();
		if (StringUtils.isEmpty(msg)) {
			msg = ResultCode.BAD_REQUEST.getMsg();
		}
		data.setMsg(msg);
		return data;
	}

	@ExceptionHandler({ Exception.class })
	public ResultData<Void> cauthException(Exception e) {
		log.error(e.getMessage(), e);
		ResultData<Void> data = new ResultData<Void>();
		data.setCode(ResultCode.BAD_REQUEST.getCode());
		data.setMsg(ResultCode.BAD_REQUEST.getMsg());
		return data;
	}
}
